<?php

namespace app\index\controller;

use app\index\model\Member;
use app\index\model\Reflect as ReflectModel;
use function json;
use function session;

class Reflect extends Basic
{

    protected $user_id;

    public function __construct()
    {
        parent::__construct();
        $this->user_id = session('member_id');
    }

    public function incr_reflect()
    {
        $member_model = Member::with([
                    'reflect' => function($query) {
                        $query->whereTIme('create_time', 'today');
                    }
                ])->find($this->user_id);
        $reflect_collection = $member_model->reflect;
        $reflect_one_arr = [
            'member_id' => $this->user_id,
            'count' => 1,
        ];
        if ($reflect_collection->isEmpty())
        {
            $reflect_model = ReflectModel::create($reflect_one_arr);
            $reflect_one_arr = $reflect_model->toArray();
        }
        else
        {
            $reflect_one_arr = $reflect_collection->toArray()[0];
            $reflect_one_arr['count'] += 1;
            ReflectModel::update($reflect_one_arr);
        }
        return json([
            'errorCode' => 0,
            'data' => $reflect_one_arr['count'],
        ]);
    }

}
